package com.blinddate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blinddate.pojo.entity.ShopInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.service.ShopInfService;
import com.blinddate.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shopInf")
@Slf4j
@Api(tags = "店面相关接口")
public class ShopInfController {
    @Autowired
    private ShopInfService shopInfService;

    @Autowired
    private UserService userService;

    @GetMapping("get_shop_list")
    @ApiOperation("获取店面信息列表")
    public Result<List<ShopInf>> getShopList(@RequestParam Integer page,@RequestParam Integer size){
        log.info("获取店面信息列表,参数page:{},size:{}",page,size);
        IPage<ShopInf> ipage = new Page<>(page,size);
        shopInfService.page(ipage);
        List<ShopInf> records = ipage.getRecords();
        return Result.success(records);
    }

    @PostMapping("/sub_shopInf")
    @ApiOperation("提交店面信息")
    public Result<String> subShopInf(@RequestBody ShopInf shopInf){
        log.info("提交店面信息,参数shopInf:{}",shopInf);
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }

        boolean save = shopInfService.save(shopInf);
        if (save){
            return Result.success("店面信息提交成功");
        }
        return Result.error("店面信息提交失败");
    }

    @PostMapping("/update_shopInf")
    @ApiOperation("更新店面信息")
    public Result<String> updateShopInf(@RequestBody ShopInf shopInf){
        log.info("更新店面信息,参数shopInf:{}",shopInf);
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }
        ShopInf shopInf1 = shopInfService.getById(shopInf.getId());
        if (shopInf1==null){
            return Result.error("没有该店面信息，更行失败");
        }

        boolean update = shopInfService.updateById(shopInf);
        if (update){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @GetMapping("/get_one_shop")
    @ApiOperation("获取详细店面信息")
    public Result<ShopInf> getOneShop(@RequestParam Long shopId){
        log.info("获取详细店面信息,参数shopId:{}",shopId);
        ShopInf shopInf = shopInfService.getById(shopId);
        return Result.success(shopInf);
    }

}
